import {Component} from '@angular/core';
import {App, NavController} from 'ionic-angular';


//导入三级跳转源页面
//import {addTrenchPage} from '../addTrench'

//actionsheet需要导入的组件
import {Platform} from 'ionic-angular';
//接受从上个页面传递过来的数据
import {NavParams} from 'ionic-angular';
import {Http} from '@angular/http';
import {interfaceUrls} from '../../../../provider/serverUrls'
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/timeout';
//产品信息数据
import {ModalController} from 'ionic-angular';
import {productPage2} from './productPage/productPage2'
import {services} from "../../../../providers/service";
import {AppPopup} from "../../../../util/popup";
import {UserService} from "../../../../providers/user-service";
import {UserLogin} from "../../../../modules/app-user/user-login/user-login";

@Component({
    selector: 'page-existDealer',
    templateUrl: 'existDealer.html'
})
export class existDealerPage {
    data: any;
    companyName;     //公司名称
    textarea;       //详细地址信息
    remarks;        //对代理产品进行备注
    name;
    product;        //产品信息
    param = {};
    // private headers = new Headers({'Content-Type': 'application/json;chartset=utf-8'});

    isShow: boolean;
    isNeed: boolean;

    constructor(public navCtrl: NavController,
                public platform: Platform,
                private navParams: NavParams,
                public http: Http,
                private app: App,
                private userServ: UserService,
                private popup: AppPopup,
                public modalCtrl: ModalController,
                public services: services) {
//	    	 this.setCityPickerData();
        this.data = this.navParams.get('data');
        console.info(this.data);
        this.product = this.navParams.get('product')
    };

    ionViewWillEnter() {
        this.userServ.getPersonInfo()
            .then(() => {
            })
            .catch(err => {
                console.error("用户详情获取失败：", err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.app.getRootNav().setRoot(UserLogin);
                        });
                }
            });
    }

    addBack() {
        this.navCtrl.pop();
    }

    addNext() {

    }


    //选择代理酒产品的菜单  (click)="openMenu()"  方法

    //当前页面取消按钮的方法
    cancel() {
        this.navCtrl.popToRoot();
    }

    confirm() {
        if (!this.services.productId2) {

        } else {

        this.http
            .post(interfaceUrls.dealerAdd, {
                    "companyName": this.data.companyName,
                    "mobile": this.data.mobile,
                    "name": this.data.name,
                "companyAddressId": this.data.companyAddressId,
                "productId": this.services.productId2,
                    "remarks": this.remarks
            })
            .timeout(4000)
            .toPromise()
            .then((response) => {
                console.info(response + "+++++++已存在经销商已添加到后台", this.remarks, this.data.companyAddressId);
                let data = response.json();
                if(data.resultStatus == "success") {
                    this.navCtrl.popToRoot();       //确定之后将数据添加到经销商页面
                }else {
                    this.popup.alert(data.errorMassage);
                }
            })
            .catch((err) => {
                console.info(err)
            })

//          console.info("====",this.areadata.city,this.areadata.city.value);

        }

    }

    openModal() {

        let modal = this.modalCtrl.create(productPage2, {product: this.product});
        modal.present();
    }


}




